/*
	* PageButton.fx
	 *
	 * Created on 16/05/2009, 11:39:45
	 */

	package webooksfx_desktop.view.components;

import javafx.scene.Cursor;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextOrigin;

/**
 * @author matias
 */

public class PageButton extends CustomNode {
	public var colorBack: Color = Color.BLACK;
	public var colorBackHover: Color = Color.GRAY;
	public var colorText: Color = Color.WHITE;
	public var text: String = "";
	public var valor: Integer;
	public var x: Integer = 0;
	public var y: Integer = 0;

	public var action: function(i:Integer) : Void;

	var click: Boolean = false;
	var color_back: Color = colorBack;
	var xText: Integer = if (text.length() == 1) 6 else if (text.length() > 1) 2 else 0;
	var yText: Integer = 5;

	public override function create(): Node {
		return Group {
			translateX: bind x
			translateY: bind y
			cursor: Cursor.HAND
			content: [
				Rectangle {
					x: 0, 
					y: 0
					width: 20,
					height: 20
					fill: bind color_back
					arcWidth: 8,
					arcHeight: 8
				},
				Text {
					font: Font {
						size: 12
					}
					fill: bind colorText
					textOrigin: TextOrigin.TOP
					translateX: bind xText
					translateY: bind yText
					content: bind text
				}
			]

			// Manjeadores de eventos sobre el grupo.
			onMousePressed: function( e: MouseEvent ):Void {
				click = true;
			}
			onMouseReleased: function( e: MouseEvent ):Void {
				if (click and hover and valor != 0) {
					this.action(valor);
				}
				click = false;
			}
			onMouseEntered: function( e: MouseEvent ):Void {
				color_back = colorBackHover;
			}
			onMouseExited: function( e: MouseEvent ):Void {
				color_back = colorBack;
			}
		};
	}
}